/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.gitee.clarence.util;

import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

import com.gitee.clarence.common.constant.Constant;

/**
 * 
 * @ClassName: BrowseUtil
 * @Description: 提供浏览器查看的功能
 * @author luopeng
 * @date 2017年8月27日 下午6:09:01
 * @version 1.0
 */
public class BrowseUtil {

	/**
	 * 私有构造函数，防止创建该类
	 */
	private BrowseUtil() {

	}

	/**
	 * 打开浏览器
	 *
	 * @param url
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void open(String url) throws Exception {
		// 获取操作系统的名字
		String osName = System.getProperty("os.name", "");
		if (osName.startsWith("Mac OS")) {
			// 苹果的打开方式
			Class fileMgr = Class.forName("com.apple.eio.FileManager");
			Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
			openURL.invoke(null, new Object[] { url });
		} else if (osName.startsWith("Windows")) {
			// windows的打开方式。
			Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
		} else {
			// Unix or Linux的打开方式
			String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
			String browser = null;
			for (int count = 0; count < browsers.length && browser == null; count++) {
				// 执行代码，在brower有值后跳出，
				// 这里是如果进程创建成功了，==0是表示正常结束。
				if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) {
					browser = browsers[count];
				}
			}
			if (browser == null) {
				throw new Exception("Could not find web browser");
			} else {
				// 这个值在上面已经成功的得到了一个进程。
				Runtime.getRuntime().exec(new String[] { browser, url });
			}
		}
	}
	
	public static void swagger(ApplicationContext context) throws Exception {

		Environment environment = context.getEnvironment();
		String host = environment.getProperty("swagger.host");
		if (StringUtils.isBlank(host)) {
			host = Constant.HOST;
		}
		String port = environment.getProperty("server.port");
		if (StringUtils.isNotBlank(port)) {
			host += Constant.COLON + port;
		}
		String contextPath = environment.getProperty("server.context-path");
		if (StringUtils.isNotBlank(contextPath)) {
			host += contextPath;
		}
		open(Constant.HTTP + Constant.COLON + Constant.SLASH + Constant.SLASH + host + "/api/index.html");
		open(Constant.HTTP + Constant.COLON + Constant.SLASH + Constant.SLASH + host + "/api/doc.html");
		open("http://eureka.didispace.com/");
	}
}
